# Contributor: Mathias LANG <pro.mathias.lang@gmail.com>
# Maintainer: Mathias LANG <pro.mathias.lang@gmail.com>
pkgname=dmd
pkgver=2.100.0
pkgrel=0
pkgdesc="D Programming Language reference compiler"
url="https://github.com/dlang/dmd"
# TODO: Enable on x86 once LDC-1.20.1 is out (and enabled on x86)
arch="x86_64"
license="BSL-1.0"
depends="llvm-libunwind-dev tzdata"
makedepends="chrpath dtools ldc"
source="dmd-$pkgver.tar.gz::https://github.com/dlang/dmd/archive/v$pkgver.tar.gz
	druntime-$pkgver.tar.gz::https://github.com/dlang/druntime/archive/v$pkgver.tar.gz
	phobos-$pkgver.tar.gz::https://github.com/dlang/phobos/archive/v$pkgver.tar.gz

	dmd-install-config.conf
	10-dmd-musl.patch
	"
builddir="$srcdir"

prepare() {
	# The Makefiles make some assumption about the directory structure
	ln -s "$srcdir/dmd-$pkgver/" "$srcdir/dmd"
	ln -s "$srcdir/druntime-$pkgver/" "$srcdir/druntime"
	ln -s "$srcdir/phobos-$pkgver/" "$srcdir/phobos"

	default_prepare
}

build() {
	./dmd/src/build.d dmd HOST_DMD="ldmd2"
	make -C "druntime/" -f posix.mak ENABLE_RELEASE=1 INSTALL_DIR="$srcdir/install" install
	make -C "phobos/" -f posix.mak ENABLE_RELEASE=1 INSTALL_DIR="$srcdir/install" install
	./dmd/src/build.d unittest HOST_DMD="dmd/generated/linux/release/64/dmd"

	# Strip redundant rpath to avoid warnings in the builder
	chrpath -d "dmd/generated/linux/release/64/dmd"
}

check() {
	dmd/generated/linux/release/64/dmd-unittest
}

package() {
	mkdir -p "$pkgdir/usr/bin/" "$pkgdir/usr/lib/" "$pkgdir/etc/" "$pkgdir/usr/include/dmd/"

	mv dmd/generated/linux/release/64/dmd "$pkgdir"/usr/bin/dmd
	cp dmd-install-config.conf "$pkgdir"/etc/dmd.conf
	mv install/linux/lib64/* "$pkgdir"/usr/lib/

	mv install/src/druntime/import/ "$pkgdir"/usr/include/dmd/druntime
	mv install/src/phobos/ "$pkgdir"/usr/include/dmd/phobos/
}

sha512sums="
e9315c8a1d31fb876b18e7e1941a04ee1cda8065f87ac9ac9335f43d7d42c43116037c5124222559753699cbe3952a706862287746cb176ac988d8a873fc4faa  dmd-2.100.0.tar.gz
68b0088dafb0e7001241b86ad884a71a302e2fc925662db394b7d428d57606897dffb5e0041a03592e4b6850311176f5d286ab98d1f157572c65e0311cccf1ab  druntime-2.100.0.tar.gz
7a719dbd90ab3eb4de46c8259b8017e07360a41eb6a8c95eceedf811037c08a3358278cc9cc3e0245e7d0df50922129ccff2de88d99576d7ae808215afc8653d  phobos-2.100.0.tar.gz
123ec0f256a73030a5e5b4b87a7f2e0752320777b7fcd175a221807ec2917f5d6d88776c3448eab077eb7a2211dd4a3d64e3a556053b0f183eb058da437bc5da  dmd-install-config.conf
a26ae89516d4125db00205df7039c62642dc65d0505f692f910fd5ee8701a91a262e9448dfcd72ca1d84b9eead0dd12dc0ebc6b61bf6162034ad7d64f72601a3  10-dmd-musl.patch
"
